package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Transactional
@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {

    @Reference
    private ContractProductService contractProductService;

    @Reference
    private FactoryService factoryService;

    @Reference
    private ExtCproductService extCproductService;


    @Autowired
    private FileUploadUtil fileUploadUtil;

    @RequestMapping(value = "/list",name ="跳转到新增货物附件页面")
    public String list(String contractId,String contractProductId,@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "10")int pageSize){

        //构建查询条件   通过合同id 查询所有货物附件
        ExtCproductExample example = new ExtCproductExample();
        example.createCriteria().andContractIdEqualTo(contractId);
        PageInfo<ExtCproduct> pageInfo =  extCproductService.findAll(example,page,pageSize);
        request.setAttribute("page",pageInfo);

//        查询所有的货物附件生产厂家
//        构建查询条件 ctype="货物附件"
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

//        页面需要的隐藏数据 contractId
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);

        return "/cargo/extc/extc-list";
    }



  /*  @RequestMapping(value = "/toAdd",name = "跳转到新增货物附件页面")
    public String toAdd(){

        return "/cargo/extc/extc-add";
    }*/


    @RequestMapping(value = "/edit",name = "跳转到新增货物附件方法")
     public String edit(ExtCproduct extCproduct,MultipartFile productPhoto)  {
//         完成货物附件图片上传
        String imgUrl="";
        try {
            if (productPhoto!=null) {
                 imgUrl = fileUploadUtil.upload(productPhoto);//返回的是路径
            }
        } catch (Exception e) {
            e.printStackTrace();
            extCproduct.setProductImage(null);
        }
        extCproduct.setProductImage(imgUrl);



        //货物附件id 为空即新增 不为空即 保存//
         if (StringUtils.isEmpty(extCproduct.getId())) {
             //设置id 公司id  公司名
             extCproduct.setId(UUID.randomUUID().toString());
             extCproduct.setCompanyName(getCompanyName());
             extCproduct.setCompanyId(getCompanyId());
             extCproduct.setCreateTime(new Date());

             extCproductService.save(extCproduct);

         }else{
//             编辑
             extCproductService.update(extCproduct);
         }


         return "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();
     }


   @RequestMapping(value = "/toUpdate",name = "跳转到编辑货物附件页面")
   public String toUpdate(String id,String contractId,String contractProductId ){//货物附件id
       //数据回显 根据附件id  查找对应附件
       ExtCproduct extCproduct = extCproductService.findById(id);
       request.setAttribute("extCproduct",extCproduct);



       //设置隐藏域的内容
       request.setAttribute("contractId",contractId);
       request.setAttribute("contractProductId",contractProductId);

//       查询所有的货物附件生产厂家
//       构建查询条件 ctype="货物附件"
       FactoryExample factoryExample = new FactoryExample();
       factoryExample.createCriteria().andCtypeEqualTo("附件");
       List<Factory> factoryList = factoryService.findAll(factoryExample);
       request.setAttribute("factoryList",factoryList);

       return "/cargo/extc/extc-update";
   }



    @RequestMapping(value = "/delete",name = "删除货物附件")
    public String delete(String id,String contractId ,String extCproductId){

        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+extCproductId;
    }

}
